//
//  LoginTextField.m
//  WeiYiMei
//
//  Created by 李叶夫 on 2019/4/14.
//  Copyright © 2019年 liyefu. All rights reserved.
//

#import "LoginTextField.h"

@interface LoginTextField ()<UITextFieldDelegate>

@property (nonatomic, strong) UIImageView *leftView;
@property (nonatomic, strong) UILabel *leftLab;
@property (nonatomic, strong) UIButton *eye;

@end

@implementation LoginTextField

- (void)setupSubViews
{
    self.backgroundColor = kColorF2F2F2;
    self.layer.cornerRadius = kWidth(24);
    self.layer.masksToBounds = YES;
}

- (void)setKeyboardType:(UIKeyboardType)keyboardType
{
    _keyboardType = keyboardType;
    self.textField.keyboardType = keyboardType;
}

- (void)setType:(LoginTextFieldType)type
{
    _type = type;
    if (type == LoginTextFieldLoginTel) {
        [self addSubview:self.leftView];
        self.leftView.image = [UIImage imageNamed:@"phone"];
        self.textField.placeholder = @"请输入手机号码";
        self.textField.keyboardType = UIKeyboardTypeNumberPad;
        [self.leftView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(12);
            make.centerY.equalTo(self);
            make.width.equalTo(@22);
            make.height.equalTo(@22);
        }];
        [self addSubview:self.textField];
        [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.leftView.mas_right).offset(6);
            make.centerY.equalTo(self);
            make.right.equalTo(self).offset(-12);
        }];
    }
    if (type == LoginTextFieldLoginPsd) {
        [self addSubview:self.leftView];
        self.leftView.image = [UIImage imageNamed:@"password"];
        self.textField.placeholder = @"请输入密码";
        [self.leftView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(12);
            make.centerY.equalTo(self);
            make.width.equalTo(@22);
            make.height.equalTo(@22);
        }];
        
        [self addSubview:self.eye];
        [self.eye mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.right.equalTo(self).offset(-12);
            make.width.equalTo(@22);
            make.height.equalTo(@22);
        }];
        
        [self addSubview:self.textField];
        self.textField.secureTextEntry = YES;
        [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.leftView.mas_right).offset(6);
            make.centerY.equalTo(self);
            make.right.equalTo(self.eye.mas_left).offset(-6);
        }];
    }
    if (type == LoginTextFieldFindTel) {
        [self addSubview:self.leftLab];
        [self addSubview:self.textField];
        self.leftLab.text = @"手机号码";
        self.textField.placeholder = @"请输入手机号码";
        self.textField.keyboardType = UIKeyboardTypeNumberPad;
        [self.leftLab setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
        [self.leftLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(12);
            make.centerY.equalTo(self);
        }];
        [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(90);
            make.centerY.equalTo(self);
            make.right.equalTo(self).offset(-12);
        }];
    }
    if (type == LoginTextFieldFindCode) {
        [self addSubview:self.leftLab];
        [self addSubview:self.textField];
        [self addSubview:self.rightView];
        self.leftLab.text = @"验证码";
        self.textField.placeholder = @"请输入验证码";
        self.textField.keyboardType = UIKeyboardTypeNumberPad;
        [self.leftLab setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
        [self.leftLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(12);
            make.centerY.equalTo(self);
        }];
        [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(90);
            make.centerY.equalTo(self);
        }];
        [self.rightView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.right.equalTo(self);
            make.left.equalTo(self.textField.mas_right).offset(6);
            make.width.equalTo(@110);
        }];
    }
    if (type == LoginTextFieldFindPsd) {
        [self addSubview:self.leftLab];
        [self addSubview:self.textField];
        self.leftLab.text = @"新密码";
        self.textField.placeholder = @"请输入新密码";
        self.textField.secureTextEntry = YES;
        [self.leftLab setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
        [self.leftLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(12);
            make.centerY.equalTo(self);
        }];
        [self addSubview:self.eye];
        [self.eye mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.right.equalTo(self).offset(-12);
            make.width.equalTo(@22);
            make.height.equalTo(@22);
        }];
        [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(90);
            make.centerY.equalTo(self);
            make.right.equalTo(self.eye.mas_left).offset(-6);
        }];
    }
    if (type == LoginTextFieldFindPsdAgain) {
        [self addSubview:self.leftLab];
        [self addSubview:self.textField];
        self.leftLab.text = @"确认密码";
        self.textField.placeholder = @"请输入密码";
        self.textField.secureTextEntry = YES;
        [self.leftLab setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
        [self.leftLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(12);
            make.centerY.equalTo(self);
        }];
        [self addSubview:self.eye];
        [self.eye mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.right.equalTo(self).offset(-12);
            make.width.equalTo(@22);
            make.height.equalTo(@22);
        }];
        [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(90);
            make.centerY.equalTo(self);
            make.right.equalTo(self.eye.mas_left).offset(-6);
        }];
    }
}

- (void)eyeAction:(UIButton *)sender
{
    sender.selected = !sender.selected;
    if (self.eyeHandle) {
        self.eyeHandle(sender.selected);
    }
    self.textField.secureTextEntry = !sender.selected;
}

- (void)getCode:(ANGradientButton *)sender
{
    if (self.getCodeHandle) {
        self.getCodeHandle(sender);
    }
}

- (UIImageView *)leftView
{
    if (!_leftView) {
        _leftView = [[UIImageView alloc] init];
    }
    return _leftView;
}

- (UILabel *)leftLab
{
    if (!_leftLab) {
        _leftLab = [[UILabel alloc] init];
        _leftLab.font = [UIFont systemFontOfSize:15];
        _leftLab.textColor = kColor333333;
    }
    return _leftLab;
}

- (UITextField *)textField
{
    if (!_textField) {
        _textField = [[UITextField alloc] init];
        _textField.backgroundColor = [UIColor clearColor];
        _textField.font = [UIFont systemFontOfSize:15];
        _textField.delegate = self;
    }
    return _textField;
}

- (ANGradientButton *)rightView
{
    if (!_rightView) {
        _rightView = [[ANGradientButton alloc] initWithFrame:CGRectMake(0, 0, 110, 48)];
        [_rightView setTitle:@"获取验证码" forState:UIControlStateNormal];
        [_rightView setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_rightView addTarget:self action:@selector(getCode:) forControlEvents:UIControlEventTouchUpInside];
        _rightView.titleLabel.font = [UIFont systemFontOfSize:15];
    }
    return _rightView;
}

- (UIButton *)eye
{
    if (!_eye) {
        _eye = [[UIButton alloc] init];
        [_eye setImage:[UIImage imageNamed:@"eye"] forState:UIControlStateNormal];
        [_eye setImage:[UIImage imageNamed:@"eye_open"] forState:UIControlStateSelected];
        [_eye addTarget:self action:@selector(eyeAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _eye;
}

@end
